﻿using DataAccess;
using DataAccess.DTO;
using DataAccess.Models;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Agent.Controllers
{
    public class BaseController : Controller
    {
        static AgencyUsers userDAL = new AgencyUsers();
#if DEBUG
        //获取当前域名
        string domain = "18212340001.f-9.cc";
        //   string url = "zsxs.sifuku.cn";
#endif
#if !DEBUG
        string domain = XYD.Common.Request.GetHost();
#endif
        private SysConfig sysConfig;

        public SysConfig SysConfig
        {
            get
            {

                if (sysConfig == null)
                {

                    if (!RedisHelper.Exist(RedisKey.SysConfig(domain)))
                    {

                        sysConfig = new DataAccess.DAL.SysConfigDAL().GetByDomin(domain);
                        if (sysConfig == null)
                            sysConfig = new DataAccess.Models.SysConfig();

                        RedisHelper.Single_Set_Itme<SysConfig>(RedisKey.SysConfig(domain), sysConfig);
                    }
                    return RedisHelper.Single_Get_Itme<SysConfig>(RedisKey.SysConfig(domain));

                }
                return sysConfig;

            }

        }

        private AgencyUsers agencyUsers;

        public AgencyUsers AgencyUsers
        {
            get
            {

                
                if (agencyUsers == null)
                {
                    string key = RedisKey.AgencyUser(SysConfig.promoter_id.ToString(), UserSession.user_name.ToString());

                    if (!RedisHelper.Exist(key))
                    {
                        agencyUsers = new DataAccess.DAL.AgencyUsersDAL().Get(UserSession.user_id);

                        RedisHelper.Single_Set_Itme<AgencyUsers>(key, agencyUsers);
                    }
                    return RedisHelper.Single_Get_Itme<AgencyUsers>(key);

                }
                return agencyUsers;

            }

        }



        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {

           
            //获取触发当前方法的的Action所在的控制器名字  
            string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;

            if (controllerName.ToUpper() == "LOGIN" )
            {
                base.OnActionExecuting(filterContext);
            }
            else
            {

                if (UserSession == null || string.IsNullOrEmpty(UserSession.user_name))
                {
                    filterContext.Result = new RedirectResult("/u/login.html");
                }
                else
                {
                    ViewBag.NickName = UserSession.nick_name;
                    ViewBag.aid = UserSession.user_name;
                }

                base.OnActionExecuting(filterContext);

            }
            //HttpContextBase ctx = filterContext.HttpContext;
            ////判断是否存在登录cookie
            //{


            //    if (ctx.Session == null)
            //    {
            //        ClearLogin();
            //        ctx.Response.Redirect("~/login.html");
            //    }
            //    else
            //    {
            //        HttpCookie Authorization = ctx.Request.Cookies["Authorization"];
            //        if (Authorization == null)
            //        {
            //            ClearLogin();
            //            ctx.Response.Redirect("~/login.html");
            //        }
            //        else if (string.IsNullOrEmpty(Authorization.Value))
            //        {
            //            ClearLogin();
            //            ctx.Response.Redirect("~/login.html");
            //        }
            //        else
            //        {
            //            UserSession session = XYD.Common.JsonHelper.DeserializeJson<UserSession>(XYD.Common.EncodeSecurity.DecryptString(XYD.Common.Utils.UrlDecode(Authorization.Value)));
            //            ViewBag.NickName = session.nick_name;
            //            ViewBag.aid = session.user_name;
            //            if (session == null)
            //            {
            //                ClearLogin();
            //                ctx.Response.Redirect("~/login.html");
            //            }
            //            else if (session.time_stamp < XYD.Common.TimeStampHelper.GenerateTimeStamp(DateTime.Now) - 24 * 3600)
            //            {
            //                ClearLogin();
            //                ctx.Response.Redirect("~/login.html");
            //            }
            //        }
            //    }
            //}
            //判断是否有访问权限
            //{
            //    string actionName = filterContext.ActionDescriptor.ActionName;
            //    string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            //    bool boo = false;
            //    if ((actionName.ToLower() != "index" && controllerName.ToLower() != "home") && boo)
            //    {
            //        //ctx.Response.Redirect("~/authorize.html");
            //    }

            //}


            
        }

        public void ClearLogin()
        {
            HttpCookie cookie = Request.Cookies["Authorization"];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddDays(-2);
                Response.Cookies.Set(cookie);
            }
        }

        private UserSession _userSession;
        public UserSession UserSession
        {

            get
            {
                if (_userSession != null)
                {
                    return _userSession;
                }
                try
                {
                    if (System.Web.HttpContext.Current.Request.Cookies[CookieHelp.UserCookieKey] != null && !string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.Cookies[CookieHelp.UserCookieKey].Value))
                    {
                        string EncryptData = System.Web.HttpContext.Current.Request.Cookies[CookieHelp.UserCookieKey].Value;
                        UserSession curAdmin = XYD.Common.JsonHelper.DeserializeJson<UserSession>(XYD.Common.EncodeSecurity.DecryptString(EncryptData));
                        ViewBag.Admin = curAdmin;
                        _userSession = curAdmin;
                        return curAdmin;
                    }
                }
                catch (Exception)
                {
                    return null;
                }
                return null;
            }
            set
            {
                _userSession = value;
            }
        }


    }
}